// pages/tvDetails/tvDetails.js
import Toast from '../../miniprogram_npm/vant-weapp/toast/toast';

const db = wx.cloud.database();
const app = getApp();
const movies_col = db.collection('movies');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        movieId: '',
        movieLists: [],
        oneFilm: [],
        showShare: false,
        shownoShare: true,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
         // 状态栏样式
         wx.setNavigationBarColor({
            frontColor: '#000000',
            backgroundColor: '#ffffff;',
        });

        this.setData({
            movieId: options.id,
        });

        this.loadtvData();
        this.loadmovieList();

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let signId = app.globalData.userId;
        let newCode = app.globalData.code;
        if(signId !== "" && newCode !== "") {
            wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
            });
            this.setData({
                showShare: true,
                shownoShare: false,
            });
        }else{
            wx.hideShareMenu({
                menus: ['shareAppMessage', 'shareTimeline']
            }); 
        }
    },

    // 请求视频详情数据
    loadtvData: function () {
        var ids = this.data.movieId;
        movies_col.doc(ids).get().then(res => {
            this.data.oneFilm.push(res.data);
            this.setData({
                oneFilm: this.data.oneFilm
            })
          })
        // movies_col.where({
        //     _id: ids
        // }).get({
        //     success(res){
        //         // var qwe = [];
        //         // console.log(res,'9999');
        //         // qwe.push(res.data)
        //         this.setData({
        //             oneFilm: res.data[0]
        //         })
        //     }
        // })
        
    },

    // 拿到推荐数据
    async loadmovieList() {
        let res = await movies_col.get();
        this.setData({
            movieLists: res.data
        })
    },

    // 点击相关推荐视频跳转详情
    jumpPage: function (event) {
        Toast.loading({
            duration: 1000,
            message: '加载中...',
            forbidClick: true,
            onClose: () => {
              // 点击新闻列表项，携带id跳转新闻详情页面
              var id = event.currentTarget.dataset.videoid;
              wx.navigateTo({
                url: '/pages/tvDetails/tvDetails?id='+id,
              })
            },
          });
    },

    // 点击收藏事件
    collectDo: function (e) {
        var self = this;
        let signId = app.globalData.userId;
        let newCode = app.globalData.code;
        if(signId !== "" && newCode !== "") {
          var newId = e.currentTarget.dataset.videoid;
          var newIndex = e.currentTarget.dataset.index;
          var item = self.data.oneFilm[newIndex];
          item.collected = !item.collected;
    
          if(item.collected === true){
            wx.showToast({
              title: '收藏成功',
              icon: 'success',
              duration: 2000
            })        
          }else{
            wx.showToast({
              title: '取消收藏成功',
              icon: 'success',
              duration: 2000
            })        
          }
    
          movies_col.doc(newId).update({
            data:{
              collected: item.collected,
              userId: signId,
            },
            success: function(res) {
              console.log(res)
            }
          });
          self.setData({
            oneFilm: self.data.oneFilm
          });
        }else{
          wx.showToast({
            title: '未登录!',
            icon: "error",
            duration: 2000
          });   
        }
        
    },

    // 分享给微信好友
    onShareAppMessage:function() {
      var self = this;
      var titleOne = self.data.oneFilm.title
      return {
        title: titleOne,
        path: '/pages/tvDetails/tvDetails?id=' + self.data.movieId,//这里是被分享的人点击进来之后的页面
      }
    },

    // 分享到朋友圈
    onShareTimeline: function(){
        return{
            title: this.data.oneFilm.title, //字符串  自定义标题
            imageUrl:"../../images/news.png"   //图片地址
        }
    },

    // 分享朋友圈按钮
    sharePyq: function() {
      wx.showToast({
        title: '未实现',
        icon: "error",
        duration: 2000
      });  
    },

    // 未登录的分享按钮事件
    noShare: function() {
      wx.showToast({
          title: '未登录!',
          icon: "error",
          duration: 2000
        });  
  },
   
})